﻿namespace Jim.K3Cloud;

/// <summary>
/// 查询基础资料选择
/// </summary>
[Sdk("Kingdee.BOS.ServiceFacade.ServicesStub.Metadata.MetadataService.GetLookupObjects", "查询基础资料选择")]
public class KingdeeGetLookupObjectsRequest : ISdkRequest<KingdeeGetLookupObjectsResponse>,IK3CloudCustomRequest
{
    /// <summary>是否辅助资料</summary>
    public bool IsAssistData { get; set; } = false;
    /// <summary>
    /// 基础资料选择Id,LookupObjectId
    /// </summary>
    public string Id { get; set; }
    /// <summary>
    /// 基础资料选择Id列表
    /// </summary>
    public List<string> Ids { get; set; }
    /// <summary>
    /// 表单Id
    /// </summary>
    public string FormId { get; set; }
    /// <summary>
    /// 名称
    /// </summary>
    public string Name { get; set; }
    /// <summary>
    /// 基础资料类型
    /// 1:辅助资料;2:仓位;其他:基础资料
    /// </summary>
    public int BaseDataType { get; set; } = 3;

    public object QueryParams()
    {
        List<string> filterList = new List<string>();
        if (!string.IsNullOrWhiteSpace(FormId)) {
            if(FormId == "BOS_ASSISTANTDATA_SELECT")BaseDataType = 1;
            if (FormId == "BOS_FLEXVALUE_SELECT") BaseDataType = 2;
            else {
                BaseDataType = 3;
                filterList.Add($" lookups.fformid = '{FormId}'");
            }
        }
        if(!string.IsNullOrWhiteSpace(Id))
            filterList.Add($" lookups.fid = '{Id}'");

        if (Ids !=null && Ids.Count >0)
            filterList.Add($" lookups.fid in '{Ids.Join(",")}'");

        if (!string.IsNullOrWhiteSpace(Name))
        {
            string langTable = (BaseDataType == 1 || BaseDataType == 2) ? "lang" : "formslang";
            filterList.Add($" {langTable}.fname = '{Name}'");
        }
        var filter = filterList.Join(" and ");
        return new
        {
            oFilter = new {
                IsAssistData = IsAssistData,
                Filter = filter,
                BaseDataType = BaseDataType
            },
        };
    }

    public void Validate()
    {
    }
}

public class KingdeeGetLookupObjectsResponse : SdkResponse<List<K3CloudLookupData>>
{
}

/// <summary>
/// 基础资料选择类型
/// </summary>
public class K3CloudLookupData
{
    public string SubSysId { get; set; }
    public string SubSysName { get; set; }
    public string FormId { get; set; }
    /// <summary>
    /// 关联的业务对象名称
    /// </summary>
    public string Name { get; set; }
    /// <summary>
    /// 关联的业务对象多语言名称
    /// </summary>
    [JsonConverter(typeof(LocaleValueConverter))]
    public string Caption { get; set; }
    /// <summary>
    /// 关联的业务对象使用数据表格
    /// </summary>
    public string TableName { get; set; }
    /// <summary>
    /// 关联的业务对象数据表格主键字段名
    /// </summary>
    public string PkFieldName { get; set; }
    /// <summary>
    /// 代码字段名, 不同于Key属性，注意区分 (用于序时簿直接关联原表取数)
    /// </summary>
    public string NumberFieldName { get; set; }
    /// <summary>
    /// 代码字段标题
    /// </summary>   
    [JsonConverter(typeof(LocaleValueConverter))]
    public string NumberFieldCaption { get; set; }
    /// <summary>
    /// 名称字段名, 不同于Key属性，注意区分 (用于序时簿直接关联原表取数)
    /// </summary>
    public string NameFieldName { get; set; }
    /// <summary>
    /// 名称字段是否为多语言字段
    /// </summary>
    public int NameFieldIsLocale { get; set; }
    /// <summary>
    /// 基础资料控制策略类型；
    /// 可选值：0 = 不受控，1 = 共享, 2 = 分配
    /// </summary>
    public int StrategyType { get; set; }
    /// <summary>
    /// MasterId字段名, 基础资料分配后该字段值不变
    /// </summary>
    public string MasterIdFieldName { get; set; }
    /// <summary>
    /// 使用组织字段Key
    /// </summary>
    public string OrgFieldName { get; set; }
    /// <summary>
    /// 主键字段类型；
    /// 主要应用于创建基础资料储存物理字段时；
    /// </summary>
    public int PkFieldType { get; set; }
    /// <summary>
    /// 基础资料个性化逻辑控制类名
    /// </summary>
    public string ControllerClass { get; set; }
    /// <summary>
    /// 在F7的时候，我们需要进行功能权限控制；
    /// 故在此增加其信息，以免在判断的当儿去数据库中去；
    /// </summary>
    public int PermissionControl { get; set; }
    /// <summary>
    /// 缓存版本号
    /// </summary>
    public string Version { get; set; }
    /// <summary>
    /// 所在路径；
    /// 做记录用，不计入xml文件中
    /// </summary>
    public string Path { get; set; }
    /// <summary>
    /// 内码
    /// </summary>
    public string Id { get; set; }
    /// <summary>
    /// 记录创建时候的时间
    /// </summary>
    public string CDT { get; set; }
    /// <summary>
    /// 本多语言数据表的内码
    /// </summary>
    public string PkId { get; set; }
    /// <summary>
    /// 本多语言数据表的内码
    /// </summary>
    public int LocaleId { get; set; }


    /// <summary>转换为字符串</summary>
    /// <returns></returns>
    public override string ToString() => this.Name;

}